require('es6-promise').polyfill();

import axios from 'axios'

export function loadAssetById(data, id) {
  return {
    type: 'LOAD_ASSET_BY_ID',
    data,
    id
  }
}

function errorAction(error) {
  return {
    type: 'ERROR_ASSET_ID',
    error
  }
}

export function apiMsgAction(msg) {
  return {
    type: 'API_MSG',
    msg
  }
}

export function loadAssetImageByMediaIdAsync(data) {
  return function(dispatch) {
    return axios.post('/api/assets/getImage', data).then(
      resp => dispatch(loadAssetById(resp.data, data.media_id)),
      error => dispatch(errorAction(error))
    )
  }
}


export function loadAssetByMediaIdAsync(data) {
  return function(dispatch) {
    return axios.post('/api/assets/queryById', data).then(
      resp => dispatch(loadAssetById(resp.data, data.media_id)),
      error => dispatch(errorAction(error))
    )
  }
}
